package com.jinlia.show.modules.system.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MysqlDbUtils {
    private static final Logger logger = LoggerFactory.getLogger(MysqlDbUtils.class);
    private static JdbcTemplate jdbcTemplate;

    public static void newInstance(JdbcTemplate jdbcTemplateIns) {
        if (jdbcTemplate == null) {
            jdbcTemplate = jdbcTemplateIns;
        }
    }
    public static Map<String, Object> retrieveTableColumnInfo(String database, String tableName) throws SQLException {
        String sql ="select a.COLUMN_NAME,a.ORDINAL_POSITION from information_schema.COLUMNS a where a.TABLE_SCHEMA = ? and a.TABLE_NAME = ?";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql, database, tableName);
        Map<String, Object> stringObjectHashMap = new HashMap<>();
        for (Map<String, Object> map : maps) {
            stringObjectHashMap.put(map.get("COLUMN_NAME").toString(),map.get("ORDINAL_POSITION"));
        }
        return stringObjectHashMap;
    }
}
